locals {
  dds_groups = {
    myapp = {
      db_name           = "baidu-myapp-dev"
      availability_zone = "${var.REGION}c"
      vpc_id            = data.terraform_remote_state.vpc.outputs.vpc_info.id
      subnet_id         = data.terraform_remote_state.subnet.outputs.subnet_info["subnet-me212-dev-baidu_hub-db-mongodb"].id
      security_group_id = data.terraform_remote_state.vsg.outputs.vsg_info.vsg_mongodb.vsg_id
      admin_password    = "${random_password.admin_pass.result}"
      spec_code         = data.huaweicloud_dds_flavors.flavor_2c4g.flavors[0].spec_code
      users = [
        {
          user_name     = "accountmgr-rw"
          user_password = "${random_password.share_pass.result}"
          user_db_name  = "accountmgr"
          role_name     = "readWrite"
          role_db_name  = "accountmgr"
        },
        {
          user_name     = "clientlogfilemgr-rw"
          user_password = "${random_password.share_pass.result}"
          user_db_name  = "clientlogfilemgr"
          role_name     = "readWrite"
          role_db_name  = "clientlogfilemgr"
        },
        {
          user_name     = "baiduimpushserver-rw"
          user_password = "${random_password.share_pass.result}"
          user_db_name  = "baiduimpushserver"
          role_name     = "readWrite"
          role_db_name  = "baiduimpushserver"
        },
        {
          user_name     = "baiduimserver-rw"
          user_password = "${random_password.share_pass.result}"
          user_db_name  = "baiduimserver"
          role_name     = "readWrite"
          role_db_name  = "baiduimserver"
        }
      ]
    }
  }
}

resource "random_password" "admin_pass" {
  length           = 12
  special          = true
  override_special = "!@#%^*-_=+"
  numeric = true
}
resource "random_password" "share_pass" {
  length           = 12
  special          = true
  override_special = "!@#%^*-_=+"
  numeric = true
}

resource "huaweicloud_dds_instance" "dds_instance" {
  for_each = local.dds_groups
  name     = each.value.db_name

  datastore {
    type           = "DDS-Community"
    version        = "4.2"
    storage_engine = "rocksDB"
  }
  availability_zone = each.value.availability_zone
  vpc_id            = each.value.vpc_id
  subnet_id         = each.value.subnet_id
  security_group_id = each.value.security_group_id
  password          = each.value.admin_password
  mode              = "ReplicaSet"
  charging_mode     = "postPaid"
  flavor {
    type      = "replica"
    num       = 1
    storage   = "ULTRAHIGH"
    size      = 10
    spec_code = each.value.spec_code
  }
  backup_strategy {
    start_time = "02:00-03:00"
    keep_days  = 30
  }
}

resource "huaweicloud_dds_database_user" "dds_user_myapp" {
  for_each = {
    for u in local.dds_groups.myapp.users :
    u.user_name => u
  }
  instance_id = huaweicloud_dds_instance.dds_instance["myapp"].id
  name        = each.value.user_name
  password    = each.value.user_password
  db_name     = each.value.user_db_name
  roles {
    name    = each.value.role_name
    db_name = each.value.role_db_name
  }
}
